/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.pdx.cs.guavadata.query;

import edu.pdx.cs.guavadata.Channel;
import edu.pdx.cs.guavadata.Transform;
import edu.pdx.cs.guavadata.condition.Condition;
import edu.pdx.cs.guavadata.subclass.ConditionType;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author VuTran
 */
public class DropForeignKey extends Operator {

    String childTable;
    List<String> childConditionColumns;
    List<Condition> childConditions;
    List<String> childColumns;

    String parentTable;
    List<String> parentConditionColumns;
    List<Condition> parentConditions;
    List<String> parentColumns;

    public List<String> getChildColumns() {
        return childColumns;
    }

    public void setChildColumns(List<String> childColumns) {
        this.childColumns = childColumns;
    }

    public List<String> getChildConditionColumns() {
        return childConditionColumns;
    }

    public void setChildConditionColumns(List<String> childConditionColumns) {
        this.childConditionColumns = childConditionColumns;
    }

    public List<Condition> getChildConditions() {
        return childConditions;
    }

    public void setChildConditions(List<Condition> childConditions) {
        try {
            // Dummy check - all conditions are equality conditions
            for (Condition c : childConditions) {
                if (c.getCondType() != ConditionType.EQ) {
                    throw new Exception("Foreign key conditions do not support conditions that are not equality conditions.");
                }
            }
            this.childConditions = childConditions;
        } catch (Exception e) {
            System.err.println("ERROR: " + e.toString());
        }
    }

    public String getChildTable() {
        return childTable;
    }

    public void setChildTable(String childTable) {
        this.childTable = childTable;
    }

    public List<String> getParentColumns() {
        return parentColumns;
    }

    public void setParentColumns(List<String> parentColumns) {
        this.parentColumns = parentColumns;
    }

    public List<String> getParentConditionColumns() {
        return parentConditionColumns;
    }

    public void setParentConditionColumns(List<String> parentConditionColumns) {
        this.parentConditionColumns = parentConditionColumns;
    }

    public List<Condition> getParentConditions() {
        return parentConditions;
    }

    public void setParentConditions(List<Condition> parentConditions) {
        try {
            // Dummy check - all conditions are equality conditions
            for (Condition c : parentConditions) {
                if (c.getCondType() != ConditionType.EQ) {
                    throw new Exception("Foreign key conditions do not support conditions that are not equality conditions.");
                }
            }
            this.parentConditions = parentConditions;
        } catch (Exception e) {
            System.err.println("ERROR: " + e.toString());
        }
    }

    public String getParentTable() {
        return parentTable;
    }

    public void setParentTable(String parentTable) {
        this.parentTable = parentTable;
    }

    public DropForeignKey(String childTable, String parentTable) {
        this.parent = null;
        this.childTable = childTable;
        this.childConditionColumns = new ArrayList<String>();
        this.childConditions = new ArrayList<Condition>();
        this.childColumns = new ArrayList<String>();
        this.parentTable = parentTable;
        this.parentConditionColumns = new ArrayList<String>();
        this.parentConditions = new ArrayList<Condition>();
        this.parentColumns = new ArrayList<String>();
    }

    public DropForeignKey(String childTable, Collection<String> childConditionColumns,
            Collection<Condition> childConditions, Collection<String> childColumns, String parentTable,
            Collection<String> parentConditionColumns, Collection<Condition> parentConditions, Collection<String> parentColumns) {
        this.parent = null;
        this.childTable = childTable;
        this.childConditionColumns = new ArrayList<String>(childConditionColumns);
        this.childConditions = new ArrayList<Condition>(childConditions);
        this.childColumns = new ArrayList<String>(childColumns);
        this.parentTable = parentTable;
        this.parentConditionColumns = new ArrayList<String>(parentConditionColumns);
        this.parentConditions = new ArrayList<Condition>(parentConditions);
        this.parentColumns = new ArrayList<String>(parentColumns);

        try {
            // Three column checks
            if (this.childConditionColumns.size() != this.childConditions.size())
                throw new Exception("Child condition column count does not match number of child conditions.");
            if (this.parentConditionColumns.size() != this.parentConditions.size())
                throw new Exception("Parent condition column count does not match number of parent conditions.");
            if (this.childColumns.size() != this.parentColumns.size())
                throw new Exception("Number of child columns does not match number of parent columns.");
        } catch (Exception e) {
            System.err.println("ERROR: " + e.toString());
        }
    }

    @Override
    public boolean isQuery() {
        return false;
    }

    @Override
    public SqlText visit(Provider qv) {
        return qv.transform(this);
    }

    @Override
    public void visit(Channel c, String table) {
        c.mapAliases(this, table);
    }

    @Override
    public void visit(Transform t, Command c) {
        t.expand(this, c);
    }

    @Override
    public void notifyChange(Operator old, Operator newOp) {
        throw new UnsupportedOperationException("The method or operation is not implemented.");
    }

    public void addColumn(String childColumn, String parentColumn) {
        this.childColumns.add(childColumn);
        this.parentColumns.add(parentColumn);
    }

    @Override
    public Operator clone() {
        return new DropForeignKey(childTable, childConditionColumns, childConditions,
                childColumns, parentTable, parentConditionColumns, parentConditions, parentColumns);
    }


}
